#!/bin/sh
# postinst framework for openpanel packages
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

case "$1" in
    configure)
NAMEDCONF=/etc/bind/named.conf
mkdir -p /var/opencore/conf/staging/DNSDomain
mkdir -p /var/named/openpanel
mkdir -p /var/named/openpanel/slaves
chown bind:bind /var/named/openpanel/slaves
chown opencore:authd /var/opencore/conf/staging/DNSDomain
OPENPANEL_TAG="// {{{openpanel.includes"
if grep -q "$OPENPANEL_TAG" "$NAMEDCONF"; then
        echo -n ""
else
cat >> "$NAMEDCONF" << _EOF_
// Do not touch the following lines, these are recognized by the openpanel
// dnsdomain package on configure
// {{{openpanel.includes
include "/var/named/openpanel/zones.conf";
// }}}
_EOF_
echo "// Openpanel initial setup" >> /var/named/openpanel/zones.conf
[ -e /etc/apparmor.d/apparmor-profile ] && XAPPARMOR=apparmor-profile
[ -e /etc/apparmor.d/usr.sbin.named ] && XAPPARMOR=usr.sbin.named
if [ ! -z "$XAPPARMOR" ]; then
  fn="/etc/apparmor.d/.new.$XAPPARMOR"
  rm -f $fn
  cat /etc/apparmor.d/$XAPPARMOR | while read ln; do
    xln=`echo "$ln" | sed -e "s/ *$//"`
    if [ "$xln" = "}" ]; then
      echo "  /var/named/openpanel/* r," >> $fn
    fi
    echo "$ln" >> $fn
  done
  mv $fn /etc/apparmor.d/$XAPPARMOR
  invoke-rc.d apparmor restart
fi
invoke-rc.d bind9 restart
fi
if [ ! -f /var/named/openpanel/axfr.conf ]; then
cat > /var/named/openpanel/axfr.conf << _EOF_
acl openpanel-axfr {
	127.0.0.1;
};
_EOF_
fi

    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


